package controllers.games;

import java.awt.Cursor;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;

import models.games.SchiacciaArancia;
import models.util.Indexes;
import views.games.SchiacciaAranciaView;
import core.GameLauncher;
import core.GameLauncher.State;

public class SchiacciaAranciaController extends MouseAdapter implements	ActionListener {

	private SchiacciaAranciaView view;
	private SchiacciaArancia model;
	private JButton[][] coverCells;
	private JButton[][] bagCells;
	private boolean increment = true;
	private Indexes[] trueTilesIndexesB;
	private Indexes[] trueTilesIndexesC;
	private ImageIcon spremiagrumi = new ImageIcon(
			"resources\\schiacciaArancia\\squeezer.png");
	private GameLauncher gameLauncher;

	public SchiacciaAranciaController(SchiacciaArancia model,
			SchiacciaAranciaView view, GameLauncher gameLauncher) {
		this.view = view;
		this.model = model;
		this.gameLauncher = gameLauncher;
		trueTilesIndexesB = model.trueTilesBag();
		trueTilesIndexesC = model.trueTilesCover();
		coverCells = view.getCoverCells();
		bagCells = view.getBagCells();
		riempiCelle();
		view.addActionListenerToGame(this);
		view.addMouseListenerToGame(this);
	}

	public void actionPerformed(ActionEvent e) {
		JButton input = (JButton) e.getSource();
		if (input.getIcon() != null && input.getIcon().equals(spremiagrumi)) {
			System.out.println("spremiagrumi");
		} else if (input.getText().equals("Ricomincia")) {
			SchiacciaArancia newModel = new SchiacciaArancia(
					model.getCurrentUser());
			SchiacciaAranciaView newView = new SchiacciaAranciaView(2, 2);
			SchiacciaAranciaController newController = new SchiacciaAranciaController(
					newModel, newView, gameLauncher);
			gameLauncher.setGame(newModel, newView, newController);
			gameLauncher.startTransition(State.GAME);
			model.addGameToDatabase();
			System.out.println("Ricomincia");
		} else if (input.getText().equals("Menu Giochi")) {
			gameLauncher.startTransition(State.GAME_MENU);
			model.addGameToDatabase();
			System.out.println("Menu Giochi");
		} else {
			int i = Integer.parseInt("" + input.getName().charAt(0));
			int j = Integer.parseInt("" + input.getName().charAt(2));
			if (model.verify(i, j)) {
				System.out.println("CORRETTO");
				model.incrementScore();
				view.setScore(model.getScore());
				view.setBagCount(model.getBagCount());
				model.incrementBagCount();
				view.setBagCount(model.getBagCount());
				model.refresh(increment);
				refresh();
				gameLauncher.correctAnswer();
			} else {
				System.out.println("ERRORE");
				model.refresh(!increment);
				refresh();
			}
		}

	}

	private void refresh() {
		view.refresh(model.getDimX(), model.getDimY());
		trueTilesIndexesB = model.trueTilesBag();
		trueTilesIndexesC = model.trueTilesCover();
		coverCells = view.getCoverCells();
		bagCells = view.getBagCells();
		// coloraCelle();
		riempiCelle();
		view.addActionListenerToGame(this);
	}

	public void riempiCelle() {

		for (int i = 0; i < trueTilesIndexesC.length; i++) {
			coverCells[trueTilesIndexesC[i].getI()][trueTilesIndexesC[i].getJ()].setIcon(spremiagrumi);
		}
		for (int i = 0; i < trueTilesIndexesB.length; i++) {
			bagCells[trueTilesIndexesB[i].getI()][trueTilesIndexesB[i].getJ()]
					.setIcon(spremiagrumi);
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		Cursor myCursore = Toolkit
				.getDefaultToolkit()
				.createCustomCursor(
						new ImageIcon("resources\\schiacciaArancia\\orange.png")
								.getImage(), new Point(0, 0), "mio_cursore");
		view.setCursor(myCursore);
	}

}
